<%--
  Created by IntelliJ IDEA.
  User: 24588
  Date: 2022-08-31
  Time: 21:29
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
    <script type="text/javascript" src="js/jquery-3.6.1.js"></script>
    <script type="text/javascript">
        $(function () {
            $.ajax({
                url:"http://localhost:8080/Dept?action=all",
                type :"GET",
                dataType:"JSON",
                success: function (data) {
                    //找到部门的select
                    var $deptno = $("#deptno");

                    //遍历部门
                    $.each(data,function (index,dept) {
                        //在JQuery中如何遍历数组
                        var option = $(document.createElement("option"));
                        option.text(dept.dname);
                        option.val(dept.deptno);
                        $deptno.append(option);
                    })

                }


            });

            $("#MGR").blur(function () {
                var mgr = $(this).val();
                $.ajax({
                    url:"http://localhost:8080/Emp?action=checkName&ENAME="+mgr,
                    type :"GET",
                    dataType:"JSON",
                    success: function (data) {
                            if(!data){
                                alert("【" + name + "】不存在");
                                return;
                            }
                        $("#MGR").val(data.eMPNO);
                    }
                });
            });

        })
    </script>

</head>
<body>
<h3>添加员工</h3>
<form method="post" action="http://localhost:8080/Emp?action=add">
    <table border="1">
        <tr>
            <td>编号:</td>
            <td>
                <input type="text" name="EMPNO" value="7956">

            </td>
        </tr>
        <tr>
            <td>姓名:</td>
            <td>
                <%--               //给输入框绑定一个失去焦点事件--%>
                <input type="text" name="ENAME" value="哈哈" onblur="checkName(this.value)">
                <span id="nameMsg"></span>
            </td>
        </tr>

        <tr>
            <td>职位:</td>
            <td>
                <input type="text" name="JOB" value="经理">
            </td>
        </tr>

        <tr>
            <td>领导:</td>
            <td>
                <input type="text" name="MGR" id="MGR" value="7369">
            </td>
        </tr>
        <tr>
            <td> 入职时间:</td>
            <td>
                <input type="date" name="HIREDATE" value="2022-09-05">
            </td>
        </tr>
        <tr>
            <td>工资:</td>
            <td>
                <input type="text" name="SAL" value="3000">
            </td>
        </tr>
        <tr>
            <td>奖金:</td>
            <td>
                <input type="text" name="COMM" value="10">
            </td>
        </tr>
        <tr>
            <td>部门编号:</td>
            <td>
<%--                <input type="text" name="deptno" value="30">--%>
                    <select name="deptno" id="deptno">
                        <!-- 默认是空的，页面加载的时候就会给这里赋值-->
                    </select>
            </td>
        </tr>
        <tr>
            <td colspan="2">
                <input type="submit" value="添加">
                <input type="reset" value="重置">
            </td>
        </tr>
    </table>
</form>

</body>
</html>
